package edu.ynu.se.xiecheng.achitectureclass.service.Impl;

import edu.ynu.se.xiecheng.achitectureclass.dao.*;
import edu.ynu.se.xiecheng.achitectureclass.service.FourCO2Service;
import edu.ynu.se.xiecheng.achitectureclass.service.SensorDataResponse;
import edu.ynu.se.xiecheng.achitectureclass.service.SensorRequest;
import edu.ynu.se.xiecheng.achitectureclass.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class FourCO2ServiceImpl implements FourCO2Service {
    private final TokenService tokenService;

    @Autowired
    public FourCO2ServiceImpl(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @Override
    public List<SensorDataResponse> getOneCO2Data() {
        String accessToken = tokenService.getAccessToken();
        String URL1 = "https://fbox360.com/api/v2/dmon/value/get?boxNo=300023040071";
        // 创建请求体，直接使用固定的 IDs
        SensorRequest requestBody = new SensorRequest();
        requestBody.setIds(Arrays.asList(
                "327061330932536081"
        ));

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + accessToken);

        // 设置请求体和请求头
        HttpEntity<SensorRequest> requestEntity = new HttpEntity<>(requestBody, headers);

        // 创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // 发送POST请求，URL不再需要boxNo路径变量
        ResponseEntity<SensorDataResponse[]> response = restTemplate.exchange(
                URL1, HttpMethod.POST, requestEntity, SensorDataResponse[].class);

        // 如果请求成功，返回数据
        if (response.getStatusCode() == HttpStatus.OK) {
            return Arrays.asList(response.getBody());
        } else {
            throw new RuntimeException("获取传感器数据失败，状态码：" + response.getStatusCode());
        }
    }

    @Override
    public List<SensorDataResponse> getTwoCO2Data() {
        String accessToken = tokenService.getAccessToken();
        String URL1 = "https://fbox360.com/api/v2/dmon/value/get?boxNo=300023040085";
        // 创建请求体，直接使用固定的 IDs
        SensorRequest requestBody = new SensorRequest();
        requestBody.setIds(Arrays.asList(
                "327061375295689610"
        ));

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + accessToken);

        // 设置请求体和请求头
        HttpEntity<SensorRequest> requestEntity = new HttpEntity<>(requestBody, headers);

        // 创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // 发送POST请求，URL不再需要boxNo路径变量
        ResponseEntity<SensorDataResponse[]> response = restTemplate.exchange(
                URL1, HttpMethod.POST, requestEntity, SensorDataResponse[].class);

        // 如果请求成功，返回数据
        if (response.getStatusCode() == HttpStatus.OK) {
            return Arrays.asList(response.getBody());
        } else {
            throw new RuntimeException("获取传感器数据失败，状态码：" + response.getStatusCode());
        }
    }

    @Override
    public List<SensorDataResponse> getThreeCO2Data() {
        String accessToken = tokenService.getAccessToken();
        String URL1 = "https://fbox360.com/api/v2/dmon/value/get?boxNo=300023040198";
        // 创建请求体，直接使用固定的 IDs
        SensorRequest requestBody = new SensorRequest();
        requestBody.setIds(Arrays.asList(
                "327061430532576245"
        ));

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + accessToken);

        // 设置请求体和请求头
        HttpEntity<SensorRequest> requestEntity = new HttpEntity<>(requestBody, headers);

        // 创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // 发送POST请求，URL不再需要boxNo路径变量
        ResponseEntity<SensorDataResponse[]> response = restTemplate.exchange(
                URL1, HttpMethod.POST, requestEntity, SensorDataResponse[].class);

        // 如果请求成功，返回数据
        if (response.getStatusCode() == HttpStatus.OK) {
            return Arrays.asList(response.getBody());
        } else {
            throw new RuntimeException("获取传感器数据失败，状态码：" + response.getStatusCode());
        }
    }

    @Override
    public List<SensorDataResponse> getFourCO2Data() {
        String accessToken = tokenService.getAccessToken();
        String URL1 = "https://fbox360.com/api/v2/dmon/value/get?boxNo=300822040454";
        // 创建请求体，直接使用固定的 IDs
        SensorRequest requestBody = new SensorRequest();
        requestBody.setIds(Arrays.asList(
                "327061586065758011"
        ));

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + accessToken);

        // 设置请求体和请求头
        HttpEntity<SensorRequest> requestEntity = new HttpEntity<>(requestBody, headers);

        // 创建RestTemplate
        RestTemplate restTemplate = new RestTemplate();

        // 发送POST请求，URL不再需要boxNo路径变量
        ResponseEntity<SensorDataResponse[]> response = restTemplate.exchange(
                URL1, HttpMethod.POST, requestEntity, SensorDataResponse[].class);

        // 如果请求成功，返回数据
        if (response.getStatusCode() == HttpStatus.OK) {
            return Arrays.asList(response.getBody());
        } else {
            throw new RuntimeException("获取传感器数据失败，状态码：" + response.getStatusCode());
        }
    }

    @Override
    public List<SensorDataResponse> getAllCO2Data() {
        List<SensorDataResponse> allData = new ArrayList<>();
        // 调用每个方法并添加其结果到 allData 列表中
        allData.addAll(getOneCO2Data());
        allData.addAll(getTwoCO2Data());
        allData.addAll(getThreeCO2Data());
        allData.addAll(getFourCO2Data());
        return allData;
    }
}
